home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10571 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointer to non-static function
  5. Date: Fri, 08 Mar 1996 09:35:05 -0400
  6. Organization: Soylent Green is People!!
  7. Message-ID: <fcusack-0803960935050001@mudskipper.cac.psu.edu>
  8. References: <4hnsfs$cp2@nntp.ucs.ubc.ca>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <4hnsfs$cp2@nntp.ucs.ubc.ca>, jamesdf@unixg.ubc.ca (James
  12. Fairweather) wrote:
  13.  
  14. > I am attempting to make some code I've written more elegant by using a
  15. > pointer to a function.  Here's how I declare the pointer:
  16. >         double (*f)(double);
  17. > Now I'd like to make f point to one of two functions, one declared as:
  18. >         double CFunction::Calculate(double);
  19. > and the other as:
  20. >         double CEquation::Calculate(double);
  21. > Neither are static, and CEquation and CFunction are not related by
  22. > inheritance.  Nor can they be, since CEquation contains a list of
  23. > CFunctions.
  24. > At compile time, the compiler issues an error, "cannot convert from
  25. > double (CFunction::*)(double) to double (__cdecl *)(double)" and
  26. > "cannot convert from double (CEquation::*)(double) to double (__cdecl
  27. > *)(double)".
  28. > This looks suspiciously like a typecasting problem to me.  
  29.  
  30. yes and no. Don't confuse "functions" with "methods". functions
  31. (typically) have global or file scope, methods have "class scope" I guess.
  32.  
  33. You want
  34.  
  35.             double (*CFunction::f)(double);
  36.  
  37. AFAIK, you wouldn't be able to arbitrarily have it point to a method in
  38. class CEquation though.
  39.  
  40. ~Frank
  41.  -- I am Pentium of Borg.  Division is futile.  You will be approximated. --
  42.  -- PGP ID: 1C0F6685 | NCB#56 | Visit me --> http://www.tdx.org/~fcusack/ --
  43.